package vn.edu.saigontech.ncms.web.study;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.result.GradeDetail;
import vn.edu.saigontech.ncms.model.result.MidtermTest;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateMidtermTestAction extends AbstractValidator {
	private Long offeringId;
	private String[] listenings;
	private String[] grammars;
	private String[] readings;
	private String[] writings;
	private String[] pronunciations;

	public String check() {
		for(int i = 0; i < this.listenings.length; i++) {
			boolean b1 = false;
			boolean b2 = false;
			boolean b3 = false;
			boolean b4 = false;
			boolean b5 = false;
			if(this.listenings != null) {
				if(!Checker.isEmpty(this.listenings[i])) {
					if(!Checker.isDouble(this.listenings[i]) || !Checker.isInRange(Double.valueOf(this.listenings[i]), 0, 100)) {
						this.sendKeyError("validator.invalid", "listening");
					}
					else {
						b1 = true;
					}
				}
			}
			if(this.grammars != null) {
				if(!Checker.isEmpty(this.grammars[i])) {
					if(!Checker.isDouble(this.grammars[i]) || !Checker.isInRange(Double.valueOf(this.grammars[i]), 0, 100)) {
						this.sendKeyError("validator.invalid", "speaking");
					}
					else {
						b2 = true;
					}
				}
			}
			if(this.readings != null) {
				if(!Checker.isEmpty(this.readings[i])) {
					if(!Checker.isDouble(this.readings[i]) || !Checker.isInRange(Double.valueOf(this.readings[i]), 0, 100)) {
						this.sendKeyError("validator.invalid", "reading");
					}
					else {
						b3 = true;
					}
				}
			}
			if(this.writings != null) {
				if(!Checker.isEmpty(this.writings[i])) {
					if(!Checker.isDouble(this.writings[i]) || !Checker.isInRange(Double.valueOf(this.writings[i]), 0, 100)) {
						this.sendKeyError("validator.invalid", "writing");
					}
					else {
						b4 = true;
					}
				}
			}
			if(this.pronunciations != null) {
				if(!Checker.isEmpty(this.pronunciations[i])) {
					if(!Checker.isDouble(this.pronunciations[i]) || !Checker.isInRange(Double.valueOf(this.pronunciations[i]), 0, 100)) {
						this.sendKeyError("validator.invalid", "pronunciation");
					}
					else {
						b5 = true;
					}
				}
			}
			if(b1 && b2 && b3 && b4 && b5) {
				Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
				GradeDetail gradeDetail = offering.getMidtermGradeDetail();
				
				MidtermTest midtermTest = new MidtermTest();
				gradeDetail.injectGrade(midtermTest, listenings, null, grammars, readings, writings, pronunciations, i);
				
				if(!Checker.isInRange(gradeDetail.computeTotalWritternScore(midtermTest), 0, 100)) {
					this.sendKeyError("validator.invalid", "total");
				}
			}	
		}
		return this.toSuccess();
	}

	public String[] getListenings() {
		return listenings;
	}

	public void setListenings(String[] listenings) {
		this.listenings = listenings;
	}

	public String[] getGrammars() {
		return grammars;
	}

	public void setGrammars(String[] grammars) {
		this.grammars = grammars;
	}

	public String[] getReadings() {
		return readings;
	}

	public void setReadings(String[] readings) {
		this.readings = readings;
	}

	public String[] getWritings() {
		return writings;
	}

	public void setWritings(String[] writings) {
		this.writings = writings;
	}

	public String[] getPronunciations() {
		return pronunciations;
	}

	public void setPronunciations(String[] pronunciations) {
		this.pronunciations = pronunciations;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}
}
